<?php

class KTDCodeParser{
    private $XLST_DIR = "XSLT/ktdcode.xsl";
    private $HRP_XLST_DIR = "XSLT/hrp-ktdcode.xsl";
    public function __construct(){

        $this->xslDoc = new DOMDocument();
        $abs_dir = sfConfig::get('sf_root_dir')."/lib/".$this->XLST_DIR;

        $this->hrpXslDoc = new DOMDocument();
        $hrp_abs_dir = sfConfig::get('sf_root_dir')."/lib/".$this->HRP_XLST_DIR;

        if(!$this->xslDoc->load($abs_dir)){
            throw new Exception(sprintf("Impossible de charger le XSLT du KTDCode dans %s", $abs_dir));
        }

        if(!$this->hrpXslDoc->load($hrp_abs_dir)){
            throw new Exception(sprintf("Impossible de charger le XSLT du KTDCode dans %s", $hrp_abs_dir));
        }


        
        $this->proc = new XSLTProcessor();
        $this->proc->importStylesheet($this->xslDoc);

        $this->hrpProc = new XSLTProcessor();
        $this->hrpProc->importStylesheet($this->hrpXslDoc);
    }
    private $xslDoc;
    public function getParsedText($txt, $hrp = false){
        $xml_txt = '<?xml version="1.0" encoding="utf-8" ?><rp>'.$txt.'</rp>';
        
        $xml_doc = new DOMDocument();
        if(!$xml_doc->loadXML($xml_txt))
            return nl2br($txt);
        if(!$hrp)
            return nl2br($this->proc->transformToXml($xml_doc));
        else
            return nl2br($this->hrpProc->transformToXml($xml_doc));
        
    }
    public static function getSingleton()
    {
        if (!isset(self::$instance)) {
            $c = __CLASS__;
            self::$instance = new $c;
        }

        return self::$instance;
    }
    private static $instance;
}
?>
